<?php

namespace App\Manage;


use App\Models\SiteInfo;
use App\Models\User;
use App\Service\Base\Manage;

class UserManage extends Manage
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = 'user';
    }

    // 列表
    // 添加
    // 修改


    // 列表
    protected function getLists()
    {
        $opt   = $this->option();
        $page  = $opt->getData('page', 1, 'intval');
        $rows  = $opt->getData('rows', 15, 'intval');
        $start = ($page - 1) * $rows;

        // 模型
        $model = $this->checkModel($this->model);
        if($model === false){
            return $this->_returnFail($this->error);
        }

        // 模型 和 查询
        $where = $this->listWhere();
        $bang  = $this->bang;
        $lists = $model->bangField($bang)->getList($where, $rows, "id DESC", $start);

        // 头像
        $domain    = env('STYLE_HOST');
        $oldDomain = env('OLD_HOST');
        foreach($lists as &$item){
            if($item['head_img']){
                $item['head_img'] = str_replace($oldDomain, $domain, $item['head_img']);
            }
        }


        return $lists;
    }

    // 详情
    protected function toShow()
    {
        $where = $this->showWhere();
        if($where === false){
            return $this->_returnFail($this->error);
        }

        $model = $this->checkModel($this->model);
        $info  = $model->isCache(true)->oneFind($where);
        if($info === false){
            return $this->_returnFail($model->getError());
        }

        $info = is_object($info) ? $info->toArray() : $info;
        // 头像
        $domain    = env('STYLE_HOST');
        $oldDomain = env('OLD_HOST');
        if($info['head_img']){
            $info['head_img'] = str_replace($oldDomain, $domain, $info['head_img']);
        }

        return $info;
    }

    // 修改密码
    public function onPassword()
    {
        $option = $this->option();
        $_id    = $option->getData('id', 0, 'intval');
        $pass   = $option->getData('password', null, 'trim');
        $pass2  = $option->getData('password2', null, 'trim');
        if(empty($_id)){
            return $this->_returnFail('请指定操作记录');
        }
        if(empty($pass)){
            return $this->_returnFail('请填写修改后的密码！');
        }
        if($pass2 != $pass){
            return $this->_returnFail('两次输入密码不相同！');
        }

        $model  = $this->checkModel($this->model);
        $result = $model->onPassword($_id, $pass);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 软删除
    protected function toSoft()
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定删除记录');
        }

        $where = $this->delWhere();
        if($where === false){
            return $this->_returnFail($this->error);
        }

        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->oneEdit($where, ['status' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 列表查询
    protected function listWhere()
    {
        $option  = $this->option();
        $where   = [['status', '>=', 0]];
        $bang    = [];
        $email   = $option->getData('email', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if($email){
            $bang[]  = 'email';
            $where[] = ['email', '=', $email];
        }
        if($keyword){
            $bang    = false;
            $where[] = $this->orWhere('loginname|nickname|showname', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }

    // 添加之前
    protected function _before_add()
    {
        $opt  = $this->option();
        $data = [
            //'id'        => $opt->getData("id", null, "intval"),
            'loginname' => $opt->getData("loginname", null, "trim"),
            'password'  => $opt->getData("password", null, "trim"),
            'nickname'  => $opt->getData("nickname", null, "trim"),
            'showname'  => $opt->getData("showname", null, "trim"),
            'email'     => $opt->getData("email", null, "trim"),
            'status'    => $opt->getData("status", 1, "trim"),
            'head_img'  => $opt->getData('head_img', null, 'trim'),
            //'activation_key' => $opt->getData("activation_key", null, "trim"),
            //'reg_date'       => $opt->getData("reg_date", null, "trim"),
        ];

        if($data['head_img']){
            $domain    = env('STYLE_HOST');
            $oldDomain = env('OLD_HOST');
            // 头像域名替换 使用统一域名地址，方便操作
            $data['head_img'] = str_replace($domain, $oldDomain, $data['head_img']);
        }

        // 检查数据
        $userDb = new User();
        $result = $userDb->checkUser($data);
        if($result === false){
            return $this->_returnFail($userDb->getError());
        }

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            //'id'        => $opt->getData("id", null, "intval"),
            'loginname' => $opt->getData("loginname", null, "trim"),
            'password'  => $opt->getData("password", null, "trim"),
            'nickname'  => $opt->getData("nickname", null, "trim"),
            'showname'  => $opt->getData("showname", null, "trim"),
            'email'     => $opt->getData("email", null, "trim"),
            'head_img'  => $opt->getData('head_img', null, 'trim'),
            'status'    => $opt->getData("status", 1, "trim"),
            //'activation_key' => $opt->getData("activation_key", null, "trim"),
            //'reg_date'       => $opt->getData("reg_date", null, "trim"),
        ];

        if($data['head_img']){
            $domain    = env('STYLE_HOST');
            $oldDomain = env('OLD_HOST');
            // 头像域名替换 使用统一域名地址，方便操作
            $data['head_img'] = str_replace($domain, $oldDomain, $data['head_img']);
        }

        // 检查数据
        $id     = $opt->getData('id', 0, 'intval');
        $userDb = new User();
        $result = $userDb->checkUser($data, $id);
        if($result === false){
            return $this->_returnFail($userDb->getError());
        }

        return $data;
    }

}


